<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SmsLog;
use App\Constants\SmsConstant;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class SmsLogController extends AdminController
{
    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->addLink('短信配置', admin_route('sms_setting.index'));
        $tab->addLink('模板管理', admin_route('sms_tpl.index'));
        $tab->add('发送记录', $this->grid(), true);
        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SmsLog(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('member_id');
            $grid->column('type')->display(function ($type){
                return SmsConstant::TPL_TYPE_MAP[$type] ?? '';
            });
            $grid->column('phone');
            $grid->column('code');
            $grid->column('ip');
            $grid->column('extends')->display(function ($extends){
                return '<pre class="dump" style="max-width: 500px">'.json_encode(json_decode($extends, true), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
            });
            $grid->column('create_time')->display(function ($v){
                return $v ? date('Y-m-d H:i:s') : $v;
            });
            $grid->column('read_time')->display(function ($v){
                return $v ? date('Y-m-d H:i:s') : $v;
            });
            $grid->column('verify_count');

            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('member_id')->width(2);
                $filter->equal('phone')->width(2);
                $filter->equal('type')->select(SmsConstant::TPL_TYPE_MAP)->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SmsLog(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('type');
            $show->field('phone');
            $show->field('code');
            $show->field('ip');
            $show->field('extends');
            $show->field('create_time');
            $show->field('read_time');
            $show->field('verify_count');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SmsLog(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('type');
            $form->text('phone');
            $form->text('code');
            $form->text('ip');
            $form->text('extends');
            $form->text('create_time');
            $form->text('read_time');
            $form->text('verify_count');
        });
    }
}
